
    document.addEventListener('DOMContentLoaded', function() {
    // 生成随机验证码
    function generateCaptcha() {
        const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        let captcha = '';
        for (let i = 0; i < 4; i++) {
            captcha += chars.charAt(Math.floor(Math.random() * chars.length));
        }
        return captcha;
    }

    // 点击验证码刷新
    const captchaImg = document.getElementById('captchaImg');
    captchaImg.textContent = generateCaptcha();
    captchaImg.addEventListener('click', function() {
    captchaImg.textContent = generateCaptcha();
});

    // 表单提交验证
    const form = document.getElementById('forgotPasswordForm');
    form.addEventListener('submit', function(e) {
    e.preventDefault();

    // 清除之前的错误信息
    document.querySelectorAll('.error-message').forEach(el => {
    el.style.display = 'none';
});

    // 获取表单值
    const username = document.getElementById('username').value.trim();
    const nickname = document.getElementById('nickname').value.trim();
    const captchaInput = document.getElementById('captcha').value.trim();
    const captcha = captchaImg.textContent;
    const newPassword = document.getElementById('newPassword').value;
    const confirmPassword = document.getElementById('confirmPassword').value;

    let isValid = true;

    // 验证账号
    if (!username) {
    document.getElementById('usernameError').textContent = '请输入账号';
    document.getElementById('usernameError').style.display = 'block';
    isValid = false;
}

    // 验证昵称
    if (!nickname) {
    document.getElementById('nicknameError').textContent = '请输入昵称';
    document.getElementById('nicknameError').style.display = 'block';
    isValid = false;
}

    // 验证验证码
    if (!captchaInput) {
    document.getElementById('captchaError').textContent = '请输入验证码';
    document.getElementById('captchaError').style.display = 'block';
    isValid = false;
} else if (captchaInput !== captcha) {
    document.getElementById('captchaError').textContent = '验证码错误';
    document.getElementById('captchaError').style.display = 'block';
    isValid = false;
}

    // 验证密码
    if (!newPassword) {
    document.getElementById('passwordError').textContent = '请输入新密码';
    document.getElementById('passwordError').style.display = 'block';
    isValid = false;
} else if (newPassword.length < 6) {
    document.getElementById('passwordError').textContent = '密码长度不能少于6位';
    document.getElementById('passwordError').style.display = 'block';
    isValid = false;
}

    // 验证确认密码
    if (!confirmPassword) {
    document.getElementById('confirmError').textContent = '请确认密码';
    document.getElementById('confirmError').style.display = 'block';
    isValid = false;
} else if (newPassword !== confirmPassword) {
    document.getElementById('confirmError').textContent = '两次输入的密码不一致';
    document.getElementById('confirmError').style.display = 'block';
    isValid = false;
}

    // 如果所有验证通过，模拟提交
    if (isValid) {
    alert('密码重置成功！');
    // 这里可以添加实际的AJAX提交代码
    // form.submit();
}
});
});
